﻿using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;

namespace _134_使用并行LINQ_LINQ语法
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("正在初始化序列.......");
            ConcurrentQueue<结构> testList = new ConcurrentQueue<结构>();

            //以并行方式向集合添加元素
            Parallel.For(20, 30000000, n =>
              {
                  testList.Enqueue(new 结构
                  {
                      Width = n,
                      Height = n
                  });
              });
            Console.WriteLine("序列初始化完成");

            Stopwatch watch = new Stopwatch();
            watch.Restart();
            var q1 = from x in testList
                     select x.Width * x.Height;
            watch.Stop();
            Console.WriteLine("普通模式，耗时：{0}ms", watch.ElapsedMilliseconds);

            watch.Restart();
            var q2 = from x in testList.AsParallel()
                     select x.Width * x.Height;
            watch.Stop();
            Console.WriteLine("并行模式，耗时：{0}ms", watch.ElapsedMilliseconds);

        }

    }

    public struct 结构
    {
        public double Width;
        public double Height;
    }
}
